package com.pfsoft.grpcclient.task;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.pfsoft.grpc.csv.api.CsvDataServiceGrpc;
import com.pfsoft.grpc.csv.api.DataInfo;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Iterator;

/**
 * @author pengfei
 * @date 2022年07月20日 11:40
 */
@Slf4j
@Component
public class CsvDataTask implements CommandLineRunner {

    @GrpcClient("grpc-server-side")
    private CsvDataServiceGrpc.CsvDataServiceBlockingStub csvDataServiceBlockingStub;


    @Override
    public void run(String... args) throws Exception {
        log.info("定时任务开始");
        LocalDateTime startDateTime = LocalDateTime.now();

        com.google.protobuf.Empty request =com.google.protobuf.Empty.newBuilder().build();
        Iterator<DataInfo> dataInfoIterator = csvDataServiceBlockingStub.getCsvData(request);
        int i=0;
        while (dataInfoIterator.hasNext()) {
            StringBuilder sb=new StringBuilder(10);
            DataInfo datainfo = dataInfoIterator.next();
            sb.append(datainfo.getId()+"|"+datainfo.getDiagnosis()+"|"+datainfo.getRadiusMean()+"|"+datainfo.getTextureMean());
            log.info("当前条数:【{}】,当前数据:【{}】",i, sb.toString());
            i++;
        }
        LocalDateTime endDateTime = LocalDateTime.now();
        Duration between = Duration.between(startDateTime, endDateTime);
        log.info("定时任务结束,执行时间：【{}】分钟",between.toMinutes());
    }
}
